package apps.site.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import system.config.HHConfig;
import system.pojo.HHMessagePojo;
import system.pojo.HHDepartPojo;

import apps.admin.form.HHMessageForm;
import apps.site.model.HHMessageModel;
import apps.site.model.HHOurInfoModel;

import com.xyrj.exception.HHValidateException;
import com.xyrj.mvc.HHAction;
import com.xyrj.utils.HHDatetime;
import com.xyrj.utils.HHPage;


/**
 * 联系我们
 * 
 * @ClassName: HHContactusAction
 * @Description:
 * @author maoyun0903(maoyun19900818@163.com);
 * @date 2012-8-22 上午10:35:49
 * @version V1.0
 */
public class HHContactusAction extends HHAction {
	/**
	 * 初始化构造函数
	 * <p>
	 * Title: HHContactusAction
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param request
	 * @param response
	 */
	public HHContactusAction(HttpServletRequest request,
			HttpServletResponse response) {
		super(request, response);
	}

	/**
	 * 联系我们
	 * 
	 * @Title: index
	 * @Description:
	 * @param
	 * @return void
	 * @throws
	 */
	public void index() {
		// 我们团队的信息
		HHDepartPojo	ourInfoPojo  =  new HHDepartPojo();
		HHOurInfoModel ourInfoModel = new HHOurInfoModel(ourInfoPojo);
		Map contactus = ourInfoModel.getRecordById(2);
		this._view.setRequestAttr("contactus", contactus);
		// 留言列表
		HHMessagePojo messagePojo	= new HHMessagePojo();
		int perpage = 10;
		HHMessageModel messageModel = new HHMessageModel(messagePojo);
		@SuppressWarnings("rawtypes")
		List<Map> messageList = messageModel.getList(this._getPageNumber(),
				perpage);
		Long totalRows = messageModel.getTotalRows(null);
		String pageHtml = HHPage.getHtmls(
				this._getTotalPage(totalRows, perpage),
				this._view.getRequestUrl(), 0);
		this._view.setRequestAttr("title", messagePojo.modelZhName);
		this._view.setRequestAttr("seoKeywords", HHConfig.SEO_KEYWORDS);
		this._view.setRequestAttr("seoDesc", HHConfig.SEO_DESC);
		this._view.setRequestAttr("messageList", messageList);
		this._view.setRequestAttr("pageHtml", pageHtml);

		this._view.theme("contact-us.jsp");
	}
	
	/**
	 * 留言方法
	 * 
	 * @description
	 * 
	 * @title detail
	 * @return void
	 */
	public void leave_message() {
		try {
			HHMessagePojo	messagePojo	= new HHMessagePojo();
			this._assginPojoByAdd();
			this._pojo.setField("sort_num", "1");
			this._pojo.setField("pass_flag", "1");
			this._pojo.setField("top_flag", "1");
			this._pojo.setField("total_visits", "1");
			this._pojo.setField("description", "description");
			this._pojo.setField("edit_time", HHDatetime.getNow());
			this._pojo.setField("create_time", HHDatetime.getNow());
			this._pojo.setField("author", "customer");
			HHMessageForm addMessageForm = new HHMessageForm();
			addMessageForm.validate(messagePojo);
			HHMessageModel messageModel = new HHMessageModel(messagePojo);
			if (0 < messageModel.add(messagePojo)) {
				this._view.alertAndGoBack("留言成功,请等待管理员审核~");
			} else {
				this._view.alertAndGoBack("留言失败,请联系管理员~");
			}
		} catch (HHValidateException e) {
			this._view.alertAndGoBack(e.getMessage());
		}
	}

	/**
     * 赋值模块的Pojo对象
     *  
     * @description
     * 
     * @title  _assginPojo
     * @return void
     */
    @SuppressWarnings("rawtypes")
    protected void _assginPojoByAdd()
    {
    	Iterator fields 	= this._pojo.getFields();
    	while(fields.hasNext()) {
    		String field  	= (String)(fields.next());
    		if(false == this._pojo.isFileField(field) &&
    		   null != this._view.getParameter(field) &&
    		   !this._view.getParameter(field).equals("")) {
    			this._pojo.setField(field, this._view.getParameter(field));
    		}
    	}
    }
}
